﻿/*
* Copyright (c) 2010-2012 Tesla Engine Group
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/


using System;
using System.Collections.Generic;

namespace Tesla.Content.Loaders.Model.Dxs {
    public class DxsPrimitive : DxsNode {
        private int _skeletonID;
        private int _groupID;
        private Dictionary<int, List<DxsPoly>> _polys;
        private Dictionary<int, DxsMaterial> _materials;
        private List<DxsMesh> _compiledMeshes;
        private List<DxsVertex> _masterVerts;
        private bool _vertexColorEnabled = false;

        public int SkeletonID {
            get {
                return _skeletonID;
            }
            set {
                _skeletonID = value;
            }
        }

        public int GroupID {
            get {
                return _groupID;
            }
            set {
                _groupID = value;
            }
        }

        public List<DxsVertex> MasterVertexList {
            get {
                return _masterVerts;
            }
        }

        public Dictionary<int, List<DxsPoly>> Polygons {
            get {
                return _polys;
            }
        }

        public Dictionary<int, DxsMaterial> Materials {
            get {
                return _materials;
            }
        }

        public bool VertexColorEnabled {
            get {
                return _vertexColorEnabled;
            }
            set {
                _vertexColorEnabled = value;
            }
        }

        public List<DxsMesh> CompiledMeshes {
            get {
                return _compiledMeshes;
            }
        }

        public DxsPrimitive(String name, int skeletonID, int groupID)
            : base(name) {
            _skeletonID = skeletonID;
            _groupID = groupID;
            _polys = new Dictionary<int, List<DxsPoly>>();
            _materials = new Dictionary<int, DxsMaterial>();
            _masterVerts = new List<DxsVertex>();
            _compiledMeshes = new List<DxsMesh>();
        }

        public void AddPoly(DxsPoly poly) {
            if(!_polys.ContainsKey(poly.MaterialID)) {
                List<DxsPoly> list = new List<DxsPoly>();
                list.Add(poly);
                _polys.Add(poly.MaterialID, list);
            } else {
                _polys[poly.MaterialID].Add(poly);
            }
        }

        public List<DxsPoly> GetPolysByMaterial(int mid) {
            if(_polys.ContainsKey(mid)) {
                return _polys[mid];
            } else {
                return null;
            }
        }

        public void AddPolysByMaterial(List<DxsPoly> list, int mid) {
            if(!_polys.ContainsKey(mid)) {
                _polys.Add(mid, list);
            } else {
                _polys.Remove(mid);
                _polys.Add(mid, list);
            }
        }

        public void AddMaterial(DxsMaterial material) {
            if(!_materials.ContainsKey(material.MaterialID)) {
                _materials.Add(material.MaterialID, material);
            }
        }

        public void CompileMeshes(NormalGeneration normalOptions, double creaseAngle, bool swapWinding) {
            foreach(KeyValuePair<int, List<DxsPoly>> kv in _polys) {
                List<DxsPoly> polyList = kv.Value;
                if(polyList.Count > 0) {
                    DxsMesh mesh = new DxsMesh(polyList, normalOptions, creaseAngle, swapWinding);
                    _compiledMeshes.Add(mesh);
                }
            }
        }
    }
}
